package com.vercon.common.utils;

import android.animation.ValueAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;

import com.vercon.common.R;

/**
 * Created by csh on 2020/8/3
 * 天气资源返回
 */
public class WeatherUtils {

    /**
     * 根据空气质量值获取对应的等级
     *
     * @param name
     * @param data
     * @return
     */
    public static String getString(Context context, String name, String data) {
        switch (name) {
            case "AQI":
                return getAQI(context, data);
            case "PM2.5":
                return getPM25(context, data);
            case "PM10":
                return getPM10(context, data);
            case "CO":
                return getCO(context, data);
            case "NO2":
                return getNO2(context, data);
            case "O3":
                return getO3(context, data);
            case "SO2":
                return getSO2(context, data);
            default:
                return getLevel(context, 0);
        }
    }

    //获取空气质量等级
    private static String getAQI(Context context, String data) {
        if (TextUtils.isEmpty(data) || data.equals("null")) {
            return "--";
        }
        float aFloat = Float.parseFloat(data);
        if (aFloat <= 50) {
            return getLevel(context, 0);
        } else if (aFloat <= 100) {
            return getLevel(context, 1);
        } else if (aFloat <= 150) {
            return getLevel(context, 2);
        } else if (aFloat <= 200) {
            return getLevel(context, 3);
        } else if (aFloat <= 300) {
            return getLevel(context, 4);
        } else {
            return getLevel(context, 5);
        }
    }

    private static String getPM25(Context context, String data) {
        if (TextUtils.isEmpty(data) || data.equals("null")) {
            return "--";
        }
        float aFloat = Float.parseFloat(data);
        if (aFloat <= 35) {
            return getLevel(context, 0);
        } else if (aFloat <= 75) {
            return getLevel(context, 1);
        } else if (aFloat <= 115) {
            return getLevel(context, 2);
        } else if (aFloat <= 150) {
            return getLevel(context, 3);
        } else if (aFloat <= 250) {
            return getLevel(context, 4);
        } else {
            return getLevel(context, 5);
        }
    }

    private static String getPM10(Context context, String data) {
        if (TextUtils.isEmpty(data) || data.equals("null")) {
            return "--";
        }
        float aFloat = Float.parseFloat(data);
        if (aFloat <= 50) {
            return getLevel(context, 0);
        } else if (aFloat <= 150) {
            return getLevel(context, 1);
        } else if (aFloat <= 250) {
            return getLevel(context, 2);
        } else if (aFloat <= 350) {
            return getLevel(context, 3);
        } else if (aFloat <= 420) {
            return getLevel(context, 4);
        } else {
            return getLevel(context, 5);
        }
    }

    private static String getSO2(Context context, String data) {
        if (TextUtils.isEmpty(data) || data.equals("null")) {
            return "--";
        }
        float aFloat = Float.parseFloat(data);
        if (aFloat <= 150) {
            return getLevel(context, 0);
        } else if (aFloat <= 500) {
            return getLevel(context, 1);
        } else if (aFloat <= 650) {
            return getLevel(context, 2);
        } else if (aFloat <= 800) {
            return getLevel(context, 3);
        } else if (aFloat <= 1600) {
            return getLevel(context, 4);
        } else {
            return getLevel(context, 5);
        }
    }

    private static String getNO2(Context context, String data) {
        if (TextUtils.isEmpty(data) || data.equals("null")) {
            return "--";
        }
        float aFloat = Float.parseFloat(data);
        if (aFloat <= 100) {
            return getLevel(context, 0);
        } else if (aFloat <= 200) {
            return getLevel(context, 1);
        } else if (aFloat <= 700) {
            return getLevel(context, 2);
        } else if (aFloat <= 1200) {
            return getLevel(context, 3);
        } else if (aFloat <= 2340) {
            return getLevel(context, 4);
        } else {
            return getLevel(context, 5);
        }
    }

    private static String getO3(Context context, String data) {
        if (TextUtils.isEmpty(data) || data.equals("null")) {
            return "--";
        }
        float aFloat = Float.parseFloat(data);
        if (aFloat <= 160) {
            return getLevel(context, 0);
        } else if (aFloat <= 200) {
            return getLevel(context, 1);
        } else if (aFloat <= 300) {
            return getLevel(context, 2);
        } else if (aFloat <= 400) {
            return getLevel(context, 3);
        } else if (aFloat <= 800) {
            return getLevel(context, 4);
        } else {
            return getLevel(context, 5);
        }
    }

    private static String getCO(Context context, String data) {
        if (TextUtils.isEmpty(data) || data.equals("null")) {
            return "--";
        }
        float aFloat = Float.parseFloat(data);
        if (aFloat <= 50) {
            return getLevel(context, 0);
        } else if (aFloat <= 100) {
            return getLevel(context, 1);
        } else if (aFloat <= 150) {
            return getLevel(context, 2);
        } else if (aFloat <= 200) {
            return getLevel(context, 3);
        } else if (aFloat <= 300) {
            return getLevel(context, 4);
        } else {
            return getLevel(context, 5);
        }
    }


    private static String getLevel(Context context, int num) {
        switch (num) {
            case 1:
                return context.getString(R.string.weather_good);
            case 2:
                return context.getString(R.string.weather_mild);
            case 3:
                return context.getString(R.string.weather_moderate);
            case 4:
                return context.getString(R.string.weather_severe);
            case 5:
                return context.getString(R.string.weather_serious);
            case 6:
            default:
                return context.getString(R.string.weather_optimal);
        }
    }

    /**
     * 获取温度单位标识
     *
     * @return
     */
    public static String getUnit() {
//        return PreferenceUtils.getUnitsData(PreferenceUtils.unit_set).equals(Constants.UNITS_METRIC) ? "℃" : "℉";
        return "℃";

    }

    /**
     * 设置动画
     *
     * @param context
     * @param parentHeight 父类控件高度
     * @param ivAqi        ImageView
     * @param apiValue     aqi空气质量值
     */
    public static void setAqiHeight(Context context, float parentHeight, ImageView ivAqi, int apiValue) {
        ivAqi.setBackgroundResource(getAqiBg(apiValue));

        ViewGroup.LayoutParams layoutParams = ivAqi.getLayoutParams();
        apiValue = (int) getAqiBgHeight(context, parentHeight, apiValue);
        ValueAnimator animator = ValueAnimator.ofInt(0, apiValue);
        int finalApiValue = apiValue;
        animator.addUpdateListener(animation -> {
//            LogUtils.d((int) animation.getAnimatedValue() + " " + finalApiValue);

            int height = (int) animation.getAnimatedValue();
            if (height < finalApiValue) {
                layoutParams.height = height;
                ivAqi.setLayoutParams(layoutParams);
            } else {
                animation.cancel();
            }
        });
        animator.setInterpolator(new OvershootInterpolator());
        animator.setDuration(3000);
        animator.start();
    }

    private static int getAqiBg(float aqiValue) {
        if (aqiValue <= 50) {
            return R.drawable.ic_aqi_optimal_bg;
        } else if (aqiValue <= 100) {
            return R.drawable.ic_aqi_good_bg;
        } else if (aqiValue <= 150) {
            return R.drawable.ic_aqi_mild_bg;
        } else if (aqiValue <= 200) {
            return R.drawable.ic_aqi_moderate_bg;
        } else if (aqiValue <= 300) {
            return R.drawable.ic_aqi_severe_bg;
        } else {
            return R.drawable.ic_aqi_serious_bg;
        }
    }

    public static void setMainAqiHeight(Context context, float parentHeight, ImageView ivAqi, int apiValue) {
        ivAqi.setBackgroundResource(getMainAqiBg(apiValue));

        ViewGroup.LayoutParams layoutParams = ivAqi.getLayoutParams();
        apiValue = (int) getAqiBgHeight(context, parentHeight, apiValue);
        ValueAnimator animator = ValueAnimator.ofInt(0, apiValue);
        int finalApiValue = apiValue;
        animator.addUpdateListener(animation -> {
//            LogUtils.d((int) animation.getAnimatedValue() + " " + finalApiValue);

            int height = (int) animation.getAnimatedValue();
            if (height < finalApiValue) {
                layoutParams.height = height;
                ivAqi.setLayoutParams(layoutParams);
            } else {
                animation.cancel();
            }
        });
        animator.setInterpolator(new OvershootInterpolator());
        animator.setDuration(3000);
        animator.start();
    }

    private static int getMainAqiBg(float aqiValue) {
        if (aqiValue <= 50) {
            return R.drawable.ic_aqi_main_optimal_bg;
        } else if (aqiValue <= 100) {
            return R.drawable.ic_aqi_main_good_bg;
        } else if (aqiValue <= 150) {
            return R.drawable.ic_aqi_main_mild_bg;
        } else if (aqiValue <= 200) {
            return R.drawable.ic_aqi_main_moderate_bg;
        } else if (aqiValue <= 300) {
            return R.drawable.ic_aqi_main_severe_bg;
        } else {
            return R.drawable.ic_aqi_main_serious_bg;
        }
    }

    private static float getAqiBgHeight(Context context, float parentHeight, int aqi) {
        return parentHeight;
    }





    public static int getDrawableId(String type, int id) {
//        LogUtils.i("getResId", type);
        if (type.contains("到")) {
            type = type.substring(type.indexOf("到")+1);
        }
        if (type.contains("转")) {
            type = type.substring(type.indexOf("转")+1);
        }
        LogUtils.i("getResId", type);
        int resId = R.mipmap.weather_duoyu;
        switch (type) {
            case "晴":
            case "热":
            case "有风":
            case "微风":
            case "和风":
            case "清风":
                resId = R.mipmap.weather_qing;
                break;
            case "少云":
            case "晴间多云":
                resId = R.mipmap.weather_duoyu;
                break;
            case "多云":
            case "阴":
                resId = R.mipmap.weather_yintian;
                break;
            case "阵雨":
            case "强阵雨":
                resId = R.mipmap.weather_zhenyu;
                break;
            case "雷阵雨":
            case "强雷阵雨":
                resId = R.mipmap.weather_leizhenyu;
                break;
//            case "雷阵雨伴有冰雹":
//            case "雷阵雨并伴有冰雹":
//                resId = R.drawable.weather_leizhenyu_bingbao;
//                break;
            case "雨雪天气":
            case "雨夹雪":
            case "阵雨夹雪":
                resId = R.mipmap.weather_yu_xue;
                break;
            case "小雨":
            case "毛毛雨/细雨":
            case "雨":
                resId = R.mipmap.weather_xiaoyu;
                break;
            case "中雨":
                resId = R.mipmap.weather_zhongyu;
                break;
            case "大雨":
                resId = R.mipmap.weather_dayu;
                break;
            case "暴雨":
                resId = R.mipmap.weather_baoyu;
                break;
            case "大暴雨":
                resId = R.mipmap.weather_dabaoyu;
                break;
            case "特大暴雨":
                resId = R.mipmap.weather_tedabaoyu;
                break;
            case "阵雪":
                resId = R.mipmap.weather_zhenxue;
                break;
            case "小雪":
            case "雪":
            case "冷":
                resId = R.mipmap.weather_xiaoxue;
                break;
            case "中雪":
                resId = R.mipmap.weather_zhongxue;
                break;
            case "大雪":
                resId = R.mipmap.weather_daxue;
                break;
            case "暴雪":
                resId = R.mipmap.weather_baoxue;
                break;
            case "雾":
                resId = R.mipmap.weather_wu;
                break;

        }
        return resId;
    }

    /**
     * 设置天气 lottie images 资源
     * @param type
     * @param id
     * @return
     */
    public static String setLottieAminaImageRes(String type, int id){
        return getLottieResId(type,id) + "images";
    }

    /**
     * 设置天气 lottie json 资源
     * @param type
     * @param id
     * @return
     */
    public static String setLottieAminaJson(String type, int id){
        return getLottieResId(type,id) + "data.json";
    }

    /**
     * 设置默认天气 lottie json 资源
     * @return
     */
    public static String setDefaultLottieAminaJson(){
        return "weather/gold/weather_yintian/data.json";
    }

    /**
     * 设置默认天气 lottie images 资源
     * @return
     */
    public static String setDefaultLottieAminaImageRes(){
        return "weather/gold/weather_yintian/images";
    }

    /**
     * 根据不同天气类型返回对应的图标
     *
     * @param type 天气类型描述，id 天气类型id，暂不用
     * @return
     */
    public static String getLottieResId(String type, int id) {
        if (type.contains("转")) {
            type = type.substring(type.indexOf("转"));
        }
        String basePath = "weather/" + "gold";
        String resId = basePath + "/weather_duoyu/";
        switch (type) {
            case "晴":
            case "热":
            case "有风":
            case "微风":
            case "和风":
            case "清风":
                resId = basePath + "/weather_qing/";
                break;
            case "少云":
            case "晴间多云":
            case "多云":
            case "阴":
                resId = basePath + "/weather_yintian/";
                break;
            case "阵雨":
            case "强阵雨":
                resId = basePath + "/weather_zhenyu/";
                break;
            case "雷阵雨":
            case "强雷阵雨":
                resId = basePath + "/weather_leizhenyu/";
                break;
            case "雨雪天气":
            case "雨夹雪":
            case "阵雨夹雪":
                resId = basePath + "/weather_yu_xue/";
                break;
            case "小雨":
            case "毛毛雨/细雨":
            case "雨":
                resId = basePath + "/weather_xiaoyu/";
                break;
            case "中雨":
                resId = basePath + "/weather_zhongyu/";
                break;
            case "大雨":
                resId = basePath + "/weather_dayu/";
                break;
            case "暴雨":
                resId = basePath + "/weather_baoyu/";
                break;
            case "大暴雨":
                resId = basePath + "/weather_dabaoyu/";
                break;
            case "特大暴雨":
                resId = basePath + "/weather_tedabaoyu/";
                break;
            case "阵雪":
                resId = basePath + "/weather_zhenxue/";
                break;
            case "小雪":
            case "雪":
            case "冷":
                resId = basePath + "/weather_xiaoxue/";
                break;
            case "中雪":
                resId = basePath + "/weather_zhongxue/";
                break;
            case "大雪":
                resId = basePath + "/weather_daxue/";
                break;
            case "暴雪":
                resId = basePath + "/weather_baoxue/";
                break;
            case "雾":
                resId = basePath + "/weather_wu/";
                break;

        }
        switch (id) {
            case 800:
                resId = basePath + "/weather_qing/";
                break;
            case 801:
            case 802:
                resId = basePath + "/weather_duoyu/";
                break;
            case 803:
            case 804:
                resId = basePath + "/weather_yintian/";
                break;
            case 521:
            case 522:
                resId = basePath + "/weather_zhenyu/";
                break;
            case 210:
            case 211:
            case 212:
                resId = basePath + "/weather_leizhenyu/";
                break;
            case 612:
            case 613:
            case 615:
            case 616:
                resId = basePath + "/weather_yu_xue/";
                break;
            case 500:
                resId = basePath + "/weather_xiaoyu/";
                break;
            case 501:
                resId = basePath + "/weather_zhongyu/";
                break;
            case 502:
                resId = basePath + "/weather_dayu/";
                break;
            case 503:
                resId = basePath + "/weather_baoyu/";
                break;
            case 504:
                resId = basePath + "/weather_dabaoyu/";
                break;
            case 511:
                resId = basePath + "/weather_dongyu/";
                break;
            case 620:
            case 621:
            case 622:
                resId = basePath + "/weather_zhenxue/";
                break;
            case 600:
                resId = basePath + "/weather_xiaoxue/";
                break;
            case 601:
                resId = basePath + "/weather_zhongxue/";
                break;
            case 602:
                resId = basePath + "/weather_baoxue/";
                break;
            case 611:
                resId = basePath + "/weather_wu/";
                break;
            case 721:
                resId = basePath + "/weather_mai/";
                break;
            case 761:
                resId = basePath + "/weather_fu_chen/";
                break;
            case 731:
                resId = basePath + "/weather_yang_sha/";
                break;
            case 751:
                resId = basePath + "/weather_sha_chen_bao/";
                break;
        }
        return resId;
    }
}
